package DAO

import (
	"CSMS/database"
	"database/sql"
	"fmt"
	"github.com/gin-gonic/gin"
)

type Consignment struct {
	ConsignmentNo     int            `db:"ConsignmentNo"`
	ConsignmentName   sql.NullString `db:"ConsignmentName"`
	ConsignmentUserNo int            `db:"ConsignmentUserNo"`
	ConsignmentAdr    sql.NullString `db:"ConsignmentAdr"`
	ConsignmentTel    sql.NullString `db:"ConsignmentTel"`
}

//SelectUserById 通过订单ID选择订单内容
func (cons *Consignment) SelectConsignmentByLoginId() bool {
	row := database.DB.QueryRow("SELECT * FROM consignment WHERE ConsignmentUserNo= ?", cons.ConsignmentUserNo)
	err := row.Scan(&cons.ConsignmentNo, &cons.ConsignmentName, &cons.ConsignmentUserNo, &cons.ConsignmentAdr, &cons.ConsignmentTel)
	if err != nil {
		fmt.Printf("QueryRow failed,err:%v\n", err)
		return false
	}
	return true
}

func SelectConsignmentInfo(indentNo int) gin.H {
	var UserNo int
	var UserName sql.NullString
	var ConsignmentName sql.NullString
	row := database.DB.QueryRow("SELECT indent.UserNo,user.UserName,consignmentName FROM consignment,indent,user WHERE indent.indentNo= ? AND	indent.UserNo=user.UserNo AND user.UserNo=consignment.ConsignmentUserNo", indentNo)
	err := row.Scan(&UserNo, &UserName, &ConsignmentName)
	if err != nil {
		fmt.Printf("QueryRow failed,err:%v\n", err)
		return nil
	}
	return gin.H{
		"UserNo":          UserNo,
		"UserName":        UserName,
		"ConsignmentName": ConsignmentName,
	}
}
